import axios from 'axios'
import storage from "../utils/storage"
const request = axios.create({
    baseURL: '/api',
    timeout: 2000,
    withCredentials: true
})
console.log(process)
request.interceptors.request.use(function (config) {
    config.headers.authorization = storage.getItem('token')
    if (config.params) {
      Object.keys(config.params).forEach(key => {
        if (!config.params[key]) {
          delete config.params[key]
        }
      })
    }
    return config
}, function (error) {
    return Promise.reject(error)
})

request.interceptors.response.use(function (response) {
    // 对响应数据做点什么
    const data = response.data;
    if (data.code === 0) {
      return Promise.reject(data);
    }
    return response.data
  }, function (error) {
    if (error.response) {
      return Promise.reject(error.response.data)
    } else {
      console.log(error)
      return Promise.reject(error)
    }
  })

export default {
    ...request,
    get: (url, params) => request.get(url, {
        params
    })
}
